//
//  BaseTabBarViewController.m
//  EveryDay
//
//  Created by lizq on 15/8/14.
//  Copyright (c) 2015年 lizq. All rights reserved.
//

#define NormalColor  [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1]
#define SelectColor  [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]
#define TabBarColor  [UIColor colorWithRed:47/255.0 green:150/255.0 blue:60/255.0 alpha:1]
#define TabBarShadow [UIColor colorWithRed:202/255.0 green:214/255.0 blue:219/255.0 alpha:1]

#import "BaseTabBarViewController.h"

@interface BaseTabBarViewController ()
@property(nonatomic,strong)TabBarButton *lastButton;
@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self hideSubTabbar];
}

//隐藏父类的tab bar
- (void)hideSubTabbar {
    for(UIView *subview in self.view.subviews) {
        if([subview isKindOfClass:[UITabBar class]]){
            [subview setHidden:YES];
        }
    }
}

//添加新的tabbar按钮
- (void)setTabBarWithItems:(NSArray *)items {
    
    CGSize viewsize = [[UIScreen mainScreen] bounds].size;
    UIView *tabbarBackground = [[UIView alloc] init];
    [tabbarBackground setFrame:CGRectMake(0, viewsize.height - 49, viewsize.width, 49)];
    [tabbarBackground setBackgroundColor:TabBarColor];
    [self.view addSubview:tabbarBackground];
    
    UIImageView *shadowLine = [[UIImageView alloc] init];
    [shadowLine setFrame:CGRectMake(0, -1, viewsize.width, 1)];
    [shadowLine setBackgroundColor:TabBarShadow];
    [tabbarBackground addSubview:shadowLine];

    CGFloat titleWidth = viewsize.width / items.count;
    
    for(int i = 0; i< items.count; i++) {
        BaseTabBarItem *item = [items objectAtIndex:i];
        TabBarButton *barBtn = [[TabBarButton alloc] init];
        [barBtn setFrame:CGRectMake(0,0, 32, 38)];
        [barBtn setCenter:CGPointMake(titleWidth/2+i*titleWidth, 25)];
        [barBtn setImage:[UIImage imageNamed:item.defIcon] forState:UIControlStateNormal];
        [barBtn setImage:[UIImage imageNamed:item.selIcon] forState:UIControlStateSelected];
        barBtn.tag = i;
        [barBtn addTarget:self action:@selector(clickTabBarButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [barBtn.title setFont:[UIFont systemFontOfSize:12]];
        [barBtn.title setFrame:CGRectMake(0, 0, 50, 21)];
        [barBtn.title setCenter:CGPointMake(32/2, 38/2 + 10)];
        [barBtn.title setTextAlignment:NSTextAlignmentCenter];
        [barBtn.title setText:item.title];
        
        if(i == 0){
            self.lastButton = barBtn;
            [self.lastButton setSelected:YES];
        }
        [tabbarBackground addSubview:barBtn];
    }
}

- (void)clickTabBarButton:(TabBarButton *)btn {
    [self.lastButton setSelected:NO];
    self.lastButton = btn;
    [self.lastButton setSelected:YES];
    self.selectedIndex = self.lastButton.tag;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@implementation BaseTabBarItem

- (instancetype)initWithDef:(NSString *)def sel:(NSString *)sel title:(NSString *)title {
    self = [super init];
    if(self) {
        self.defIcon = def;
        self.selIcon = sel;
        self.title = title;
    }
    return self;
}

@end

@implementation TabBarButton
- (instancetype)init {
    self = [super init];
    if(self) {
        self.title = [[UILabel alloc] init];
        [self addSubview:self.title];
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [self.title setTextColor:selected?SelectColor:NormalColor];
}

@end
